package com.zyh.alpacaDemo.orderAdaper;


import com.zyh.alpacaDemo.order.AlpacaOrderService;
import com.zyh.alpacaDemo.order.pojo.OrderRequest;
import com.zyh.alpacaDemo.order.pojo.OrderResponse;

/**
 * @Classname AlpacaTradingAdapter
 * @Description TODO()
 * @Date 2025-09-01 21:50
 * @Created by 86177
 */
public class AlpacaTradingAdapter implements TradingService{

    private final AlpacaOrderService alpacaOrderService;

    public AlpacaTradingAdapter() {
        this.alpacaOrderService = new AlpacaOrderService();
    }
    @Override
    public AdapterOrderResponse createOrder(AdapterOrderRequest request) throws Exception {
        // 转换统一请求到 Alpaca 请求
        OrderRequest alpacaRequest = new OrderRequest();
        alpacaRequest.setSymbol(request.getSymbol());
        alpacaRequest.setSide(request.getSide());
        alpacaRequest.setType(request.getType());
        alpacaRequest.setTimeInForce(request.getTimeInForce());
        alpacaRequest.setLimitPrice(request.getLimitPrice());
        alpacaRequest.setStopPrice(request.getStopPrice());

        // 发送请求并获取响应
        OrderResponse alpacaResponse = alpacaOrderService.createOrder(alpacaRequest);

        // 转换响应为统一格式
        AdapterOrderResponse response = new AdapterOrderResponse();
        response.setId(alpacaResponse.getId());
        response.setStatus(alpacaResponse.getStatus());
        response.setSymbol(alpacaResponse.getSymbol());
        response.setSide(alpacaResponse.getSide());
        response.setFilledQty(Double.valueOf(alpacaResponse.getFilledQty()));

        return response;
    }
}
